package ui.panels
{
	import flash.display.Sprite;
	import flash.utils.setTimeout;
	
	import global.Global;
	
	import gs.TweenLite;
	
	import ui.prompt.PromptBox;
	
	import utils.DisplayUtil;
	
	import worlds.BaseWorld;
	
	public class PanelManager
	{
		private static const ANIMATE_TIME			: Number = 0.2;// 动画时间，秒为单位
		
		private var _promptBox						: PromptBox;
		private var _recipeDetailPanel				: RecipeDetailPanel;
		private var _orderedDishPanel				: OrderedDishPanel;
		
		private var _panelDic						: Object;
		private var _mask							: Sprite;
		private var _panelStack						: Array = [];
		
		private var _animateLock					: Boolean = false;
		
		private var _currentPanel					: AbstractPanel = null;
		
		public function get openning():Boolean {
			return (_currentPanel != null);
		}
		public function get currentPanel():AbstractPanel {
			return _currentPanel;
		}
		
		public function PanelManager()
		{
		}
		public function init():void {
			_panelDic = {};
			_mask = new Sprite();
			with(_mask.graphics) {
				beginFill(0, 0.3);
				drawRect(0, 0, Global.SCREEN_WIDTH, Global.SCREEN_HEIGHT);
				endFill();
			}
			_mask.visible = false;
			_mask.alpha = 0;
			BaseWorld.getInstance().panelLayer.addChild(_mask);
			
			_promptBox = PromptBox.getInstance();
			_recipeDetailPanel = new RecipeDetailPanel();
			_orderedDishPanel = new OrderedDishPanel();
			
			addPanel(_promptBox);
			addPanel(_recipeDetailPanel);
			addPanel(_orderedDishPanel);
		}
		private function addPanel(panel:AbstractPanel):void {
			_panelDic[panel.type] = panel;
			panel.visible = false;
			panel.alpha = 0;
			BaseWorld.getInstance().panelLayer.addChild(panel);
		}
		private function animateCompleteHandler():void {
			_animateLock = false;
		}
		
		public function getPanel(type:int):AbstractPanel {
			return _panelDic[type];
		}
		public function open(type:int):void {
			if(_animateLock) {
				setTimeout(open, ANIMATE_TIME * 1000, type);
				return;
			}
			_animateLock = true;
			if(_currentPanel != null) {
				_panelStack.push(_currentPanel);
				_currentPanel = null;
			}
			var panel:AbstractPanel = getPanel(type);
			_currentPanel = panel;
			panel.x = (Global.STAGE_WIDTH - panel.width) / 2;
			panel.y = (Global.STAGE_HEIGHT - panel.height) / 2;
			_mask.visible = true;
			panel.visible = true;
			DisplayUtil.moveToTop(_mask);
			DisplayUtil.moveToTop(panel);
			panel.openHandler();
			TweenLite.to(_mask, ANIMATE_TIME, {alpha:1, onComplete:animateCompleteHandler});
			TweenLite.to(panel, ANIMATE_TIME, {alpha:1})
		}
		public function close(type:int):void {
			if(_animateLock) {
				setTimeout(close, 100, type);
				return;
			}
			_animateLock = true;
			var panel:AbstractPanel = getPanel(type);
			TweenLite.to(_mask, ANIMATE_TIME, {alpha:0});
			TweenLite.to(panel, ANIMATE_TIME, {alpha:0});
			setTimeout(function():void {
				_mask.visible = false;
				panel.visible = false;
				panel.closeHandler();
				if(_currentPanel == null && _panelStack.length > 0) {
					_currentPanel = _panelStack.pop();
					DisplayUtil.moveToTop(_mask);
					DisplayUtil.moveToTop(_currentPanel);
				}
				_animateLock = false;
			}, ANIMATE_TIME * 1000);
			if(_currentPanel == panel) _currentPanel = null;
		}
		public function closeAll():void {
			var panel:AbstractPanel;
			while(_panelStack.length > 0) {
				panel = _panelStack.pop();
				panel.visible = false;
				panel.closeHandler();
			}
			if(_currentPanel != null) {
				_currentPanel.visible = false;
				_currentPanel.closeHandler();
				_currentPanel = null;
			}
			_mask.visible = false;
		}
	}
}